package com.zgjkhis.modules.system.service.impl;

import com.github.pagehelper.PageInfo;
import com.zgjkhis.common.service.impl.BaseServiceImpl;
import com.zgjkhis.domain.PageResult;
import com.zgjkhis.modules.charges.domain.vo.ExpenseDetail;
import com.zgjkhis.modules.charges.service.dto.InfoDrugCostDto;
import com.zgjkhis.modules.his.service.dto.DataPrescription;
import com.zgjkhis.modules.his.service.dto.DataPrescriptionDetail;
import com.zgjkhis.modules.statistics.resp.DrugRefundResp;
import com.zgjkhis.modules.system.domain.InfoDrug;
import com.zgjkhis.modules.system.domain.vo.DrugSaleVo;
import com.zgjkhis.modules.system.req.RefundReq;
import com.zgjkhis.modules.system.service.InfoDrugService;
import com.zgjkhis.modules.system.service.mapper.InfoDrugMapper;
import com.zgjkhis.utils.FileUtil;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;

/**
 * @Author yzq
 * @Description 处方详情
 * @Date 2021/6/23 9:47
 * @Param
 * @return
 **/
@Service
//@CacheConfig(cacheNames = "user")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class InfoDrugServiceImpl extends BaseServiceImpl<InfoDrugMapper, InfoDrug> implements InfoDrugService {

    @Override
    public List<ExpenseDetail> queryExpense(String orderNo) {
        return baseMapper.queryExpense(orderNo);
    }

    @Override
    public List<InfoDrug> queryInfoDrugList(String orderNo) {
        return baseMapper.queryInfoDrugList(orderNo);
    }

    @Override
    public List<InfoDrugCostDto> queryTotalFeeForInfo(String orderNo) {
        return baseMapper.queryTotalFeeForInfo(orderNo);
    }

    @Override
    public int queryInfoDrugByInfoNo(String infoNo) {
        return baseMapper.queryInfoDrugByInfoNo(infoNo);
    }

    @Override
    public List<InfoDrug> queryInfoDrugListByInfoNo(String infoNo) {
        return baseMapper.queryInfoDrugListByInfoNo(infoNo);
    }

    @Override
    public  List<DrugSaleVo> drugSale(String pharmacyId, String startDate, String endDate) {
        return baseMapper.drugSale(pharmacyId,startDate,endDate);
    }

    @Override
    public PageResult<DrugSaleVo> drugStock(String pharmacyId, String drugId, String startDate, String endDate, String userName, String docName, String type, Pageable pageable) {
        getPage(pageable);
        PageInfo<DrugSaleVo> page = new PageInfo<>(baseMapper.drugStock(pharmacyId,drugId, startDate, endDate, userName, docName,type));
        return PageResult.data(page);
    }

    @Override
    public void outStockDetails(String pharmacyId,String drugId,String startDate,String endDate,String userName,String docName,String type, HttpServletResponse response) throws IOException {

        List<DrugSaleVo> allList = baseMapper.drugStock(pharmacyId,drugId, startDate, endDate, userName, docName,type);

        List<Map<String, Object>> list = new ArrayList<>();
        for (DrugSaleVo vo : allList) {
            Map<String, Object> map = new LinkedHashMap<>();
            switch(Integer.valueOf(vo.getDeliveryType())) {
                case 1:
                    type="科室领用";
                    break;
                case 2:
                    type="报损";
                    break;
                case 3:
                    type="员工取药";
                    break;
                case 4:
                    type="门店调拨";
                    break;
                case 5:
                    type="盘点出库";
                    break;
                case 10:
                    type="处方用药";
                    break;
                default:
                    type="未知";
                    break;
            }
            map.put("入库单号", vo.getOrderNo());
            map.put("领用人", vo.getPatientName());
            map.put("操作人", vo.getDocName());
            map.put("出库方式", type);
            map.put("出库时间", vo.getAdjustTime());
            map.put("出库数量", vo.getQuantity());
            map.put("报损数量", vo.getBs());
            map.put("批次号", vo.getBatchNo());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public PageResult<DrugRefundResp> refundList(RefundReq refundReq, Pageable pageable) {
        getPage(pageable);
        List<DrugRefundResp> drugRefundResps = baseMapper.refundList(refundReq);
        PageInfo<DrugRefundResp> page = new PageInfo<>(drugRefundResps);
        return PageResult.data(page);
    }

    @Override
    public void outRefundList(RefundReq refundReq, HttpServletResponse response) throws IOException {
        List<DrugRefundResp> drugRefundResps = baseMapper.refundList(refundReq);
        List<Map<String, Object>> list = new ArrayList<>();
        for (DrugRefundResp vo : drugRefundResps) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("药品名称",vo.getDrugName());
            map.put("退款金额",vo.getRefundTotal());
            map.put("退款数量",vo.getRefundCount());
            map.put("订单时间",vo.getCreateTime());
            map.put("退款时间",vo.getRefundTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public List<DataPrescription> queryDataList() {
        return baseMapper.queryDataList();
    }

    @Override
    public List<DataPrescriptionDetail> queryDataDetailList(String id, BigDecimal quantityDay) {
        return baseMapper.queryDataDetailList(id,quantityDay);
    }

    @Override
    public List<DataPrescription> queryInfoByOrderNo(String orderNo) {
        return baseMapper.queryInfoByOrderNo(orderNo);
    }
}
